package com.sirap.fish.fetchers;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.component.Konstants;
import com.sirap.basic.tool.C;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.Colls;
import com.sirap.basic.util.EmptyUtil;
import com.sirap.basic.util.IOUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.common.domain.Album;
import com.sirap.extractor.images.FenunFetcher;

public class FenunImageFetchers {

	private static final int TOP_COUNT = 6;
	public static final List<String> TOP_LADIES = Lists.newArrayList();
	static {
		TOP_LADIES.add("易阳");
		TOP_LADIES.add("赵伊彤");
		TOP_LADIES.add("温心怡");
		TOP_LADIES.add("妮可");
		TOP_LADIES.add("妲己");
		TOP_LADIES.add("杨晨晨");
		TOP_LADIES.add("刘飞儿");
		TOP_LADIES.add("巨乳");
		TOP_LADIES.add("XIUREN");
		TOP_LADIES.add("尤果圈");
		TOP_LADIES.add("美媛馆");
		TOP_LADIES.add("尤蜜荟");
		TOP_LADIES.add("尤物馆");
	}
	
	public static void main(String[] args) {
		Object va = null;
//		va = getFenunAlbums(3);
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
		String sa = "https://www.fenun.cn/Item/List?keyword=赵伊彤";
		sa = "https://www.fenun.cn/Item/Details/20912";
		va = getAlbum(sa);
//		va = getAlbumsByPage(1); //627
//		va = getAlbumsByPage(1, "易阳"); //2
//		va = getAlbumsByPage(12, "易阳"); //2
//		va = getAlbumsByPage(1, "赵伊彤"); //1
//		va = getAlbumsByPage(1, "赵伊彤你"); //0
//		va = getAlbumsByPage(36, "巨乳"); //36
//		va = getAlbumsByPage(13, "巨乳"); //36
		D.pjsp(va);
	}
	
	public static Map<String, Object> getAlbumsByPage(int page) {
		return getAlbumsByPage(page, "");
	}
	
	public static Map<String, Object> getAlbumsByPage(int page, String keyword) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
	        	String urlsports = "https://www.fenun.cn/Item/List?Page=" + page;
	        	if(!EmptyUtil.isNullOrEmpty(keyword)) {
	        		urlsports += "&Keyword=" + encodeURLParam(keyword.trim());
	        	}
				showFetching();
				return urlsports;
			}
//			@Override
			protected void fetchx() {
				String dir = "E:/KDB/tasks/1122_Tears/home.txt";
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			
			private String maxpage() {
				String regex = "<div class=\"pagination-container\">(.+?)</div>";
				String kids = StrUtil.findFirstMatchedItem(regex, source);
				//contains >>
				Matcher ma = createMatcher("<a href=\"[^\"]+Page=(\\d+)[^\"]+\">>></a>", kids);
				String max = null;
				if(ma.find()) {
					max = ma.group(1);
					D.pl("a " + max);
				} else {
					ma = createMatcher(">(\\d+)<", kids);
					while(ma.find()) {
						max = ma.group(1);
					}
					if(max != null) {
						D.pl("b " + max);
					} else {
						String temp = getPrettyText(kids);
						if(EmptyUtil.isNullOrEmpty(temp)) {
							max = "0";
						}
						D.pl("c " + max);
					}
				}
				D.pl(max);
				if(max == null) {
					
				}
//				if(temp == null) {
//					temp = getPrettyText(kids);
//					if(EmptyUtil.isNullOrEmpty(temp)) {
//						temp = "0";
//					}
//				}
//				D.pl("max " + temp);
				return max;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				
				String total = maxpage();
				item.put("totalpage", total);

				String regex = "<div class=\"box-item\">(.+?</a>\\s*</p>\\s*)</div>";
				Matcher ma = createMatcher(regex);
				String homeurl = "https://www.fenun.cn";
				
				while(ma.find()) {
					String sec = ma.group(1);
					String href = homeurl + StrUtil.findFirstMatchedItem("href=\"([^\"]+)\"", sec);
					String cover = StrUtil.findFirstMatchedItem("src=\"([^\"]+)\"", sec);
					String regex2 = "<p class=\"item-name\">(.+?)</p>";
					String temp = StrUtil.findFirstMatchedItem(regex2, sec);

					Map<String, Object> row = Amaps.createMap("href", href);
					row.put("cover", cover);
					row.put("title", getPrettyText(temp));
					row.put("tags", tagsFromList(TOP_LADIES));

					mexItems.add(row);
				}
				
				item.put("photos", mexItems);
				C.listSome(mexItems, 3);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Album getAlbum(String albumurl) {
		Album item = FenunFetcher.of(albumurl);
		
		return item;
	}

	public static List<String> tagsFromList(List<String> data) {
		List<String> keys = Lists.newArrayList(data);
		Collections.shuffle(keys);
		
		return Colls.top(keys, TOP_COUNT);
	}
	
	public static Integer countOfImages(String albumurl) {
		Extractor<Integer> frank = new Extractor<Integer>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				String regex = "data-large-src=\"(.+?)\"";
				Matcher ma = createMatcher(regex);
				item = 0;
				while(ma.find()) {
					item++;
				}
			}
		};
		
		return frank.process().getItem();
	}
}
